<?php
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class igphotoViewIgphoto extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		
		$configArray =& JComponentHelper::getParams('com_igallery');
		$moderate = $configArray->get('moderate_img', 0);
    	
        $context = 'igphoto';
        $catid		        = $mainframe->getUserStateFromRequest( $context.'.catid', 'catid',	0, 'int' );
		$filter_state		= $mainframe->getUserStateFromRequest( $context.'.filter_state',		'filter_state',		'',				'word' );
		$filter_order		= $mainframe->getUserStateFromRequest( $context.'.filter_order',		'filter_order',		'ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $context.'.filter_order_Dir',	'filter_order_Dir',	'',				'word' );
		$search				= $mainframe->getUserStateFromRequest( $context.'.search',			'search',			'',				'string' );
		$search				= JString::strtolower( $search );
		
		$items		= & $this->get( 'Data');
		$total		= & $this->get( 'Total');
		$pagination = & $this->get( 'Pagination' );
		
		$title = '';
		if( !empty($catid) )
		{
			$model =& $this->getModel();
			$category = $model->getCategory($catid);
			$title = ' - '.$category->name;
		}
		
		JToolBarHelper::title( JText::_( 'MANAGE PHOTOS').$title ,'generic.png' );
		JToolBarHelper::publish();
		JToolBarHelper::unpublish();
		
		if( !empty($moderate) )
		{
			JToolBarHelper::custom('moderate', 'tick.png', 'tick.png', JText::_( 'APPROVE' ) );
			JToolBarHelper::custom('unmoderate', 'unmoderate.png', 'unmoderate.png', JText::_( 'UNAPPROVE' ) );
		}
		
		JToolBarHelper::deleteList();
		
		$lists = array();
		
		$lists['state']	= JHTML::_('grid.state',  $filter_state );
		$lists['order_Dir'] = $filter_order_Dir;
		$lists['order'] = $filter_order;
		$lists['search']= $search;
		
		$lists['catid'] = igHtmlHelper::getCategorySelect('catid', 'id', 'name', null, true, false, 1, true, $catid);
		
		if( !empty($catid) )
		{
		
			$lists['cat_id_copy_move'] = igHtmlHelper::getCategorySelect('cat_id_copy_move', 'id', 'name', $category->id, true, false, 1, false, 0, false);
			
			$selectItems     = array();
		    $selectItems[]   = JHTML::_('select.option', 'copy', JText::_('COPY') );
		    $selectItems[]   = JHTML::_('select.option', 'move', JText::_('MOVE') );
		    $lists['copy_move'] = JHTML::_('select.genericlist', $selectItems, 'copy_move', 'class="inputbox" size="1"' );
		}
	    
	    $thumbFiles = array();
	    $mainFiles = array();
		for ($i=0; $i<count($items); $i++)
		{
		    $row =& $items[$i];
		    
		    if(! $thumbFiles[$i] = igFileHelper::originalToResized($row->filename, $row->thumb_width, 
		    $row->thumb_height, $row->img_quality, $row->crop_thumbs, $row->rotation, $row->round_thumb, $row->round_fill) )
		    {
		        return false;
		    }
		    
		    if(! $mainFiles[$i] = igFileHelper::originalToResized($row->filename, $row->max_width, 
		    $row->max_height, $row->img_quality, $row->crop_main, $row->rotation, $row->round_large, $row->round_fill, 
		    $row->watermark, $row->watermark_text, $row->watermark_text_color, $row->watermark_text_size, 
		    $row->watermark_filename, $row->watermark_position, $row->watermark_transparency, 1) )
		    {
		        return false;
		    }	
		}
        
		if( !empty($catid) )
		{
			$uploader = $configArray->get('file_uploader', 'swfupload');
			$uploaderFileName = IG_UPLOAD_PATH.DS.$uploader.DS.$uploader.'.php';
			$uploaderClassName = 'igUpload'.ucfirst($uploader);
			$headJsFunctionName = $uploader.'HeadJs';
			$htmlFunctionName = $uploader.'HTML';
		
			require_once($uploaderFileName);
			call_user_func( array($uploaderClassName, $headJsFunctionName) );
			call_user_func( array($uploaderClassName, $htmlFunctionName) );
		}
		else
		{
			echo '<p style="padding: 5px 15px;">'.JText::_('PLEASE SELECT CATEGORY TO UPLOAD').'<p>';
		}
		
		$document =& JFactory::getDocument();
		$document->addStyleSheet(IG_HOST.'components/com_igallery/css/toolbar.css');
		
		$db	=& JFactory::getDBO();
		
		$this->assignRef('category',	$category);
		$this->assignRef('configArray',	$configArray);
		$this->assignRef('moderate',	$moderate);
		$this->assignRef('catid',		$catid);
		$this->assignRef('db',		    $db);
		$this->assignRef('thumbFiles',  $thumbFiles);
		$this->assignRef('mainFiles',   $mainFiles);
		$this->assignRef('lists',		$lists);
		$this->assignRef('items',		$items);
		$this->assignRef('pagination',	$pagination);
        
		parent::display($tpl);
	}
}